// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SourceFile

package com.sgame.android.network;

import com.sgame.android.MLog;
import com.sgame.android.lang.*;
import com.sgame.android.lang.Error;
import com.sgame.android.lang.ErrorMap;

import org.json.*;

// Referenced classes of package com.mobage.ww.android.network:
//            b

public abstract class HttpResponseHandler extends AsyncHttpResponseHandler
{

    public HttpResponseHandler(Object obj)
    {
    	mData = obj;
    }

    public HttpResponseHandler()
    {
    }

    public void onSuccess(String s)
    {
    	MLog.d("MobageJsonHttpResponseHandler", "onSuccess");
        try
        {
            Object j;
            int error;
            JSONObject jsonobject;
            if((j = ((getJsonObject(s)))) instanceof JSONObject)
                if(isSuccess(jsonobject = (JSONObject)j))
                {
                    if(jsonobject.has("error"))
                    {
                    	error = jsonobject.getInt("error");
                        String s1 = jsonobject.getString("error_msg");
                        onError(((Error) (new ServerError(error, s1))), jsonobject);
                        return;
                    }
                    if(jsonobject.has("Error"))
                    {
                        Object obj1;
                        error = Integer.parseInt(((JSONObject) (obj1 = jsonobject.getJSONObject("Error"))).getString("Code"));
                        obj1 = ((JSONObject) (obj1)).getString("Message");
                        onError(((Error) (new ServerError(error, ((String) (obj1))))), jsonobject);
                        return;
                    } else
                    {
                    	onError(((Error) (new ServerError(0, jsonobject.toString()))), jsonobject);
                        return;
                    }
                } else
                {
                	onSuccess((JSONObject)j);
                    return;
                }
            if(j instanceof JSONArray)
            	onSuccess((JSONArray)j);
            return;
        }
        catch(JSONException jsonexception)
        {
        	MLog.e("MobageJsonHttpResponseHandler", (new StringBuilder("fail to parse the response to JSON Object from the server response: ")).append(s).toString());
        	onFailure(((Throwable) (jsonexception)), s);
            return;
        }
    }

    public void onSuccess(JSONObject jsonobject)
    {
    	MLog.e("MobageJsonHttpResponseHandler", "onSuccess - got unexpected object.");
    	onError(new Error(ErrorMap.INVALID_DATA, new RuntimeException("Unexpectedly received an object response")), jsonobject);
    }

    public void onSuccess(JSONArray jsonarray)
    {
    	MLog.e("MobageJsonHttpResponseHandler", "onSuccess - got unexpected array.");
        try
        {
        	onError(new Error(ErrorMap.INVALID_DATA, new RuntimeException("Unexpectedly received an object response")), (new JSONObject()).put("array", jsonarray));
            return;
        }
        catch(JSONException _ex)
        {
        	onError(new Error(ErrorMap.INVALID_DATA, new RuntimeException("Unexpectedly received an object response")), new JSONObject());
        }
    }

    public abstract void onError(Error error, JSONObject jsonobject);

    public void onFailure(Throwable throwable, String s)
    {
    	Object obj;
        MLog.e("MobageJsonHttpResponseHandler", (new StringBuilder("onFailure called with responseBody: ")).append(s).toString(), throwable);
        if(s != null)
        {
            try
            {
                if((obj = ((Throwable) (getJsonObject(s)))) instanceof JSONObject)
                {
                	onError(new Error(ErrorMap.SERVER_ERROR), ((JSONObject) (obj)));
                    return;
                } else
                {
                	onError(new Error(ErrorMap.SERVER_ERROR), new JSONObject());
                    return;
                }
            }
            // Misplaced declaration of an exception variable
            catch(Exception ex)
            {
            	MLog.e("MobageJsonHttpResponseHandler", (new StringBuilder("fail to parse the response to JSON Object from the server response: ")).append(s).toString());
            }
            onError(new Error(ErrorMap.SERVER_ERROR, throwable), new JSONObject());
            return;
        } else
        {
        	onError(new Error(ErrorMap.NETWORK_UNAVAILABLE, throwable), new JSONObject());
            return;
        }
    }

    public final void a()
    {
        if(mData instanceof ExecuteHttpRequest)
        {
        }
    }

    private static boolean isSuccess(JSONObject jsonobject)
    {
        boolean flag = false;
        try
        {
            if(jsonobject.has("success"))
                flag = !jsonobject.getBoolean("success");
            else
            if(jsonobject.has("error"))
                flag = jsonobject.getBoolean("error");
            else
            if(jsonobject.has("Error"))
                flag = true;
        }
        catch(JSONException _ex) { }
        return flag;
    }

    private static Object getJsonObject(String s)
        throws JSONException
    {
        return (new JSONTokener(s)).nextValue();
    }

    private Object mData;
}
